package com.zx.fm.controller;

import com.zx.common.annotation.Log;
import com.zx.common.core.controller.BaseController;
import com.zx.common.core.domain.AjaxResult;
import com.zx.common.core.page.TableDataInfo;
import com.zx.common.enums.BusinessType;
import com.zx.common.utils.RedisSequenceGenerator;
import com.zx.common.utils.poi.ExcelUtil;
import com.zx.fm.domain.PaymentApplicationVO;
import com.zx.fm.domain.Receipt;
import com.zx.fm.domain.ReceiptVO;
import com.zx.fm.service.IReceiptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * -- 高贵的马儿驱逐体内的一切外力 --
 *
 * @author kaajee
 * @date 2025/9/5
 * @desc
 */
@RestController
@RequestMapping("/fm/rm")
public class ReceiptController extends BaseController {
    @Autowired
    private IReceiptService receiptService;

    // 注入编号生成器
    private final RedisSequenceGenerator sequenceGenerator;

    public ReceiptController(RedisSequenceGenerator sequenceGenerator) {
        this.sequenceGenerator = sequenceGenerator;
    }

    @RequestMapping("/list")
    public TableDataInfo list(ReceiptVO receiptVO) {
        startPage();
        List<ReceiptVO> list = receiptService.selectReceiptList(receiptVO);
        return getDataTable(list);
    }
    @PreAuthorize("@ss.hasPermi('fm:rm:add')")
    @Log(title = "收款管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult addReceipt(@Validated @RequestBody Receipt receipt) {
        receipt.setUserId(getUserId());
        // 生成编号：REC-20250906-001 格式
        receipt.setReceiptNo(sequenceGenerator.generateSequence("REC"));
        return toAjax(receiptService.insertReceipt(receipt));
    }
    @PreAuthorize("@ss.hasPermi('fm:rm:edit')")
    @Log(title = "收款管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/del/{id}")
    public AjaxResult delReceipt(@PathVariable Long id) {
        return toAjax(receiptService.deleteReceipt(id));
    }

    @GetMapping("/get/{id}")
    public AjaxResult getReceipt(@PathVariable Long id) {
        return success(receiptService.getReceipt(id));
    }
    @PreAuthorize("@ss.hasPermi('fm:rm:edit')")
    @Log(title = "收款管理", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult editReceipt(@RequestBody Receipt receipt) {
        return toAjax(receiptService.updateReceipt(receipt));
    }
    @PreAuthorize("@ss.hasPermi('fm:rm:export')")
    @Log(title = "收款管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ReceiptVO receiptVO) {
        List<ReceiptVO> list = receiptService.selectReceiptList(receiptVO);
        ExcelUtil<ReceiptVO> util = new ExcelUtil<>(ReceiptVO.class);
        util.exportExcel(response, list, "付款申请数据");
    }
}